package com.qiantu.phone.ui.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.qiantu.phone.R;
import com.qiantu.phone.app.AppApplication;
import com.lxj.xpopup.animator.PopupAnimator;
import com.lxj.xpopup.animator.TranslateAlphaAnimator;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.enums.PopupAnimation;
import com.lxj.xpopup.util.XPopupUtils;

public class MessagePopupDialog extends BasePopupView {

    private DialogClickListener mDialogClickListener;
    private ImageView messageIcon;
    private TextView tvMessageTitle;
    private TextView tvMessageContent;
    private String messageType;
    private String messageTitle;
    private String messageContent;

    public MessagePopupDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getInnerLayoutId() {
        return R.layout.message_popup_dialog;
    }

    // 执行初始化操作，比如：findView，设置点击，或者任何你弹窗内的业务逻辑
    @Override
    protected void onCreate() {
        super.onCreate();
        messageIcon = findViewById(R.id.message_icon);
        tvMessageTitle = findViewById(R.id.tv_message_title);
        tvMessageContent = findViewById(R.id.tv_message_content);
        FrameLayout.LayoutParams layoutParams = (LayoutParams) getPopupContentView().getLayoutParams();
        layoutParams.topMargin = XPopupUtils.getStatusBarHeight() + getResources().getDimensionPixelSize(R.dimen.dp_4);
        getPopupContentView().setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mDialogClickListener != null){
                    mDialogClickListener.onClick();
                    dismiss();
                }
            }
        });
        setMessageIconByType();
        if(!TextUtils.isEmpty(messageTitle)){
            tvMessageTitle.setText(messageTitle);
        }
        if(!TextUtils.isEmpty(messageContent)){
            tvMessageContent.setText(messageContent);
        }
    }

    private void setMessageIconByType(){
        if(TextUtils.isEmpty(messageType)){
            return;
        }
        switch (messageType){
            case "1":
                break;
            case "4":
                messageIcon.setImageDrawable(AppApplication.getInstance().getThemeDrawable(R.attr.icon_home_addlinkage));
                break;
        }
    }

    public void setMessageType(String messageType){
        this.messageType = messageType;
        if(messageIcon != null){
//            setMessageIconByType();
        }
    }

    public void setMessageTitle(String messageTitle){
        this.messageTitle = messageTitle;
        if(tvMessageTitle != null){
            tvMessageTitle.setText(messageTitle);
        }
    }

    public void setMessageContent(String messageContent){
        this.messageContent = messageContent;
        if(tvMessageContent != null){
            tvMessageContent.setText(messageContent);
        }
    }

    // 设置最大宽度，看需要而定
    @Override
    protected int getMaxWidth() {
        return super.getMaxWidth();
    }

    // 设置最大高度，看需要而定
    @Override
    protected int getMaxHeight() {
        return super.getMaxHeight();
    }

    /**
     * 弹窗的宽度，用来动态设定当前弹窗的宽度，受getMaxWidth()限制
     *
     * @return
     */
    protected int getPopupWidth() {
        return 0;
    }

    @Override
    public int getAnimationDuration() {
        return 200;
    }

    /**
     * 弹窗的高度，用来动态设定当前弹窗的高度，受getMaxHeight()限制
     *
     * @return
     */
    protected int getPopupHeight() {
        return 0;
    }

    @Override
    protected PopupAnimator getPopupAnimator() {
        return new TranslateAlphaAnimator(getPopupContentView(), getAnimationDuration(), PopupAnimation.TranslateAlphaFromTop);
    }

    @Override
    protected void doAfterShow() {
        super.doAfterShow();
        postDelayed(new Runnable() {
            @Override
            public void run() {
                dismiss();
            }
        }, 2000);
    }

    public void setDialogClickListener(@Nullable DialogClickListener listener) {
        mDialogClickListener = listener;
    }

    @Override
    protected void onShow() {
        super.onShow();
    }

    public interface DialogClickListener {
        void onClick();
    }
}
